package tp.pr5.instructions;

import tp.pr5.Constants;
import tp.pr5.NavigationModule;
import tp.pr5.Place;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.ItemContainer;

public class StormInstruction implements Instruction
{

	private NavigationModule navigation;
	private RobotEngine engine;

	public StormInstruction()
	{
		this.navigation = null;
		this.engine = null;
	}
	
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException 
	{
		cad = cad.toUpperCase();
		if(cad.equals("STORM") || cad.equals("TORMENTA"))
		{
			return new StormInstruction();
		}
		throw new WrongInstructionFormatException();
	}

	@Override
	public String getHelp()
	{
		return "STORM|TORMENTA";
	}

	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer)
	{
		this.navigation = navigation;
		this.engine = engine;
	}

	@Override
	public void execute() throws InstructionExecutionException 
	{
		Place currentPlace = this.navigation.getCurrentPlace();
		int damage = -currentPlace.storm();
		
		this.engine.addFuel(damage);
		if(damage != 0)
		{
			throw new InstructionExecutionException(Constants.MESSAGE_STORM_HIT);
		}
		this.engine.saySomething(Constants.MESSAGE_STORM_FAILS);
	}

}
